package com.hxzy.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.common.core.domain.entity.SysDictData;
import com.hxzy.common.core.domain.entity.SysRole;
import com.hxzy.common.utils.DictUtils;
import com.hxzy.system.domain.dto.SysDictDataSearchDTO;
import com.hxzy.system.mapper.SysDictDataMapper;
import com.hxzy.system.service.ISysDictDataService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 通用字典数据
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {


    @Override
    public IPage<SysDictData> search(SysDictDataSearchDTO sysDictDataSearchDTO) {
        IPage<SysRole> page= PageDTO.of(sysDictDataSearchDTO.getPageNum(),sysDictDataSearchDTO.getPageSize());
        return  this.baseMapper.search(page,sysDictDataSearchDTO);

    }

    @Override
    public List<SysDictData> searchExport(SysDictDataSearchDTO sysDictDataSearchDTO) {
        return this.baseMapper.searchExport(sysDictDataSearchDTO);
    }

    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return null;
    }

    @Override
    public List<SysDictData> selectByDictType(String dictType) {
        return this.baseMapper.selectByDictType(dictType);
    }

    /**
     * 删除值
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes)
        {
            SysDictData data = this.getById(dictCode);
            this.removeById(dictCode);
            //根据类型查询缓存中的值，并写入到redis中
            List<SysDictData> dictDatas = this.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        return this.baseMapper.selectDictDataByType(dictType);
    }
}
